package com.alquimia.identityaccess.domain.model;

import java.io.Serializable;
import java.util.regex.Pattern;

/**
 * Created by Ivo on 12/07/2014.
 */
public final class FullName implements Serializable{

    private static final long serialVersionUID = 1L;

    private String firstName;
    private String lastName;

    public FullName(String aFirstName, String aLastName) {
        super();

        setFirstName(aFirstName);
        setLastName(aLastName);
    }

    public FullName(FullName aFullName) {
        this(aFullName.firstName(), aFullName.lastName());
    }

    public String asFormattedName() {
        return this.lastName() + ", " + this.firstName();
    }


    public String lastName() {
        return this.lastName;
    }

    public String firstName() {
        return this.firstName;
    }

    public FullName withChangedFirstName(String aFirstName) {
        return new FullName(aFirstName, this.lastName());
    }

    public FullName withChangedLastName(String aLastName) {
        return new FullName(this.firstName(), aLastName);
    }


    @Override
    public boolean equals(Object anObject) {
        boolean equalObjects = false;

        if (anObject != null && this.getClass() == anObject.getClass()) {
            FullName typedObject = (FullName) anObject;
            equalObjects =
                    this.firstName().equals(typedObject.firstName()) &&
                            this.lastName().equals(typedObject.lastName());
        }

        return equalObjects;
    }

    @Override
    public int hashCode() {
        int hashCodeValue =
                +(59151 * 191)
                        + this.firstName().hashCode()
                        + this.lastName().hashCode();

        return hashCodeValue;
    }

    @Override
    public String toString() {
        return "FullName [firstName=" + firstName + ", lastName=" + lastName + "]";
    }

    protected FullName() {
        super();
    }

    private void setFirstName(final String aFirstName) {

        if (aFirstName.isEmpty())
            throw new IllegalArgumentException("Se requiere un nombre.");

        if (!Pattern.matches("[A-Z][a-z]*", aFirstName))
            throw new IllegalArgumentException("El nombre debe comenzar en mayuscula.");

        if (aFirstName.length() < 1 || aFirstName.length() > 50)
            throw new IllegalArgumentException("El apellido debe tener al menos 1 letra y no mas de 50.");

        this.firstName = aFirstName;
    }

    private void setLastName(final String aLastName) {

        if (aLastName.isEmpty())
            throw new IllegalArgumentException("Se requiere un apellido.");

        /*if (!Pattern.matches("[A-Z][a-z]*", aLastName))
            throw new IllegalArgumentException("El lastName debe comenzar en mayuscula.");*/

        if (aLastName.length() < 1 || aLastName.length() > 50)
            throw new IllegalArgumentException("El lastName debe tener al menos 1 letra y no mas de 50.");

        this.lastName = aLastName;
    }
}
